/**
 * frinder_liu
 *
 * @date 2016-04-06 09:40
 */
(function ($) {

    'use strict';

    var defaults = {
        url: '',
        tip: '请选择……',
        remote: true,
        kv: {k: 'id', v: 'text'}, // 相应字段转换为 k -> v
        searchField: '', // 搜索: model后台接收查询值字段
        likeFields: [], // 搜索字段
        textField: '' // 文本对应字段
    };

    var MySelect2 = function (element, options) {
        this.$this = element;
        this.$opts = options;
    };

    MySelect2.prototype = {
        constructor: MySelect2,
        select: function () {
            var $opts = $.extend(true, {}, defaults, this.$opts);
            var $this = this.$this;
            $this.select2({
                language: 'zh-CN',
                width: '100%',
                placeholder: $opts.tip,
                allowClear: true,
                minimumInputLength: 1,
                // multiple: true,
                maximumSelectionLength: 2,
                ajax: {
                    url: $opts.url,
                    type: 'POST',
                    contentType: "application/json;charset=UTF-8",
                    dataType: 'JSON',
                    quietMillis: 500,
                    data: function (term, page) {
                        console.log(term);
                        var p = {
                            target: {}
                        };
                        p.likeFields = $opts.likeFields;
                        p.target[$opts.searchField] = term.term;
                        console.log(p);
                        return JSON.stringify(p);
                    },
                    processResults: function (o) {
                        var data = $.map(o.data, function (m) {
                            console.log(m[$opts.kv.k], m[$opts.kv.v]);
                            return {
                                id: m[$opts.kv.k],
                                text: m[$opts.kv.v]
                            }
                        });
                        return {
                            results: data
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (m) {
                    return m;
                }
            });
        },
        change: function () {
            var e = this;
            var $this = e.$this;
            var $opts = e.$opts;
            $this.on('change', function () {
                var data = $this.select2('data');
                console.log(data);
                if (data.length > 0) {
                    var text = 'input[name="{0}"]'.format($opts.textField);
                    $(text).val(data[0].text);
                    e.selectItem = data[0];
                }
            });
        },
        selectItem: function () {
            return this.selectItem;
        }
    };

    $.fn.MySelect2 = function (options) {
        var select = new MySelect2(this, options);
        select.select();
        select.change();
        return select;
    };

    $.fn.MySelect2.Constructor = MySelect2;

})(jQuery);